extern crate fejpsl_contract;
use fejpsl_contract::banner::Banner;

pub async fn get_banner_by_id(id:&String) -> Option<Banner> {
    let client = super::db::connect().await.unwrap();
    let rows = &client.query("SELECT item_desc,id,image_path,is_visible,item_order,title,item_type,url,deleted FROM t_banner where id::text=$1", &[&id]).await.unwrap();
    let row = rows.get(0).unwrap();
    let banner = Banner {
        item_desc:   row.get(0),
        id: row.get(1),
        image_path:  row.get(2),
        is_visible:   row.get(3),
        item_order: row.get(4),
        title:  row.get(5),
        item_type:   row.get(6),
        url: row.get(7),
        deleted:  row.get(8),
    };
    return Some(banner);
}

pub async fn delete_banner_by_id(id:&String) -> Option<bool> {
    let client = super::db::connect().await.unwrap();
    let _rows = &client.query("DELETE FROM t_banner where id::text=$1", &[&id]).await.unwrap();
    return Some(true);
}

pub async fn insert_banner(item_desc:&String,image_path:&String,is_visible:&i32,item_order:&i32,title:&String,item_type:&i32,url:&String) -> Option<Banner> {
    let client = super::db::connect().await.unwrap();
    let _row = client.query("INSERT INTO t_banner VALUES(uuid_in(md5(random()::text || clock_timestamp()::text)::cstring),$1,$2,$3,$4,$5,$6,$7)",
                            &[&item_desc,&image_path,&is_visible,&item_order,&title,&item_type,&url]).await.unwrap();
    let banner = Banner {
        item_desc: String::from(item_desc),
        id: 0,
        image_path:  String::from(image_path),
        is_visible:   *is_visible,
        item_order: *item_order,
        title:  String::from(title),
        item_type:  *item_type,
        url: String::from(url),
        deleted:  false,
    };
    return Some(banner);
}

pub async fn list_banner() -> Option<Vec<Banner>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_banner = Vec::new();
    let rows = &client.query("SELECT item_desc,id,image_path,is_visible,item_order,title,item_type,url,deleted FROM t_banner", &[]).await.unwrap();
    for row in rows {
        let banner = Banner {
            item_desc:   row.get(0),
            id: row.get(1),
            image_path:  row.get(2),
            is_visible:   row.get(3),
            item_order: row.get(4),
            title:  row.get(5),
            item_type:   row.get(6),
            url: row.get(7),
            deleted:  row.get(8),
        };
        vec_banner.push(banner);
    }
    return Some(vec_banner);
}

pub async fn mocked_list_banner() -> Option<Vec<Banner>> {
    let mut vec_banner = Vec::new();

    vec_banner.push(Banner {
        item_desc:String::from("doc"),
        id:111,
        image_path:String::from("google.com"),
        is_visible:1,
        item_order:1,
        title:String::from("google.com"),
        item_type:1,
        url:String::from("google.com"),
        deleted:false,
    });
    vec_banner.push(Banner {
        item_desc:String::from("doc"),
        id:112,
        image_path:String::from("baidu.com"),
        is_visible:1,
        item_order:1,
        title:String::from("baidu.com"),
        item_type:1,
        url:String::from("baidu.com"),
        deleted:false,
    });
    return Some(vec_banner);

}